class StudentController < ApplicationController

  def index
  end

  def list
    @students = Student.get_all
  end

  def show
    @student = Student.get_by_id(params[:id])
  end

  def new
    @student = Student.new
    @address = Address.new
  end

  def create
    @student = Student.new(params[:student])
    @student.address = Address.new(params[:address])
    
    if @student.save
      flash[:notice] = 'Student successfully added'
      APPLOG.info 'Added student #' + @student.id.to_s + ' ' + @student.first_name + ' ' + @student.last_name
      redirect_to :action => 'index'
    else
      APPLOG.info 'Error saving student #' + @student.id.to_is
      render :action => 'new'
    end      
  end

  def update
    @student = Student.get_by_id(params[:id])
    @address = @student.address
    
    if @student.update_attributes(params[:student]) && @address.update_attributes(params[:address])
      flash[:notice] = 'Student successfully updated'
      APPLOG.info 'Updated student #' + @student.id.to_s + ', ' + @student.first_name + ' ' + @student.last_name
      redirect_to :action => 'index'
    else
      render :action => 'edit'
    end
  end

  def edit
    @student = Student.get_by_id(params[:id])
    APPLOG.info 'Retrieving student #' + @student.id.to_s + ', ' + @student.first_name + ' ' + @student.last_name
  end

  def deactivate
    if Student.delete(params[:id])
      APPLOG.INFO 'Deactivated student #' + params[:id].to_s
      redirect_to :action => 'index'
    else
      render :action => 'edit'
    end
  end
end
